package com.linkstec.BloomFilter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.common.hash.Funnels;
import com.linkstec.redis.RedisUtil;

import redis.clients.jedis.Jedis;

public class RedisBloomFilterTest {
	private static final Logger log = LogManager.getLogger();

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) {

		log.info("开始!!!");
		Map<String, String> cfg = new HashMap<String, String>();
		cfg.put("redisHostAndPortSingle", "localhost:6379");
		cfg.put("keyNamespace", "SMOT_");
//		cfg.put("passwd", "foobared");
		RedisUtil.setConfig(cfg);

		RedisBloomFilter redisBloomFilter = new RedisBloomFilter();
		int expectedInsertions = 1000;
		double fpp = 0.1;
		redisBloomFilter.delete("bloom");
		BloomFilterHelper<CharSequence> bloomFilterHelper = new BloomFilterHelper<>(
				Funnels.stringFunnel(Charset.defaultCharset()), expectedInsertions, fpp);
		int j = 0;
		// 添加100个元素
		List<String> valueList = new ArrayList<>();
		for (int i = 0; i < 100; i++) {
			valueList.add(i + "");
		}
		long beginTime = System.currentTimeMillis();
		redisBloomFilter.addList(bloomFilterHelper, "bloom", valueList);
		long costMs = System.currentTimeMillis() - beginTime;
		log.info("布隆过滤器添加{}个值，耗时：{}ms", 100, costMs);
		Jedis jedis = RedisUtil.getJedis();
		for (int i = 0; i < 1000; i++) {
			boolean result = redisBloomFilter.contains(jedis, bloomFilterHelper, "bloom", i + "");
			if (!result) {
				j++;
			}
		}
		jedis.close();
		log.info("漏掉了{}个,验证结果耗时：{}ms", j, System.currentTimeMillis() - beginTime);
	}
}
